package rsj.sql.user;

import com.rlyy.aibase.utils.base.MapNode;
import com.rlyy.aibase.utils.base.Node;
import rsj.annotation.Param;
import rsj.annotation.Select;
import rsj.annotation.Update;
import rsj.api.user.entity.UserAccount;

/**
 * 用户dao
 *
 * @author yuanqinglong
 * @since 2021/5/26 9:23
 */
public interface UserDao {


    /**
     * 通过id查询用户
     *
     * @param userId 用户id
     * @return {@link MapNode}
     */
    @Select("SELECT u.user_id, u.user_uname,user_org_id AS org_id ,u.user_name,u.user_email,r.role_name,r.role_id,user_password_utime " +
            "FROM sys.sys_user u LEFT JOIN sys.sys_role r ON u.user_role_id = r.role_id WHERE u.user_id = #{userId}")
    MapNode getUserByUserId(@Param("userId") String userId);


    /**
     * 通过用户名获取用户
     *
     * @param userName 用户名
     * @return {@link MapNode}
     */
    @Select("SELECT * FROM sys.sys_user WHERE user_uname = #{userName}")
    MapNode getUserByUserName(@Param("userName") String userName);


    /**
     * 查询用户的机构id
     *
     * @param userId 用户id
     * @return {@link MapNode}
     */
    @Select("SELECT user_org_id FROM sys.sys_user WHERE user_id = #{userId}")
    MapNode queryOrgIdByUser(@Param("userId") String userId);

    /**
     * 查询用户的基本信息
     *
     * @param userId 用户id
     * @return {@link MapNode}
     */
    @Select("SELECT user_name,user_uname,user_email,user_org_id FROM sys.sys_user WHERE user_id = #{userId}")
    MapNode queryBaseInfoByUser(@Param("userId") String userId);



    /**
     * 添加用户
     *
     * @param createUser 创建用户
     */
    @Update(value = "INSERT INTO sys.sys_user (user_id,user_uname,user_password,user_org_id,user_ctime,user_utime,user_role_id,user_type,user_name)" +
            " VALUES (#{userId},#{userUname},#{userPassword},#{userOrgId},#{userCtime},#{userUtime},#{userRoleId},#{userType},#{userName})")
    void addUser(UserAccount createUser);


    /**
     * 获取用户
     *
     * @param orgId 机构id
     * @return {@link MapNode}
     */
    @Select(value = "SELECT user_uname FROM sys.sys_user WHERE user_org_id = #{orgId}")
    MapNode getUser(@Param(value = "orgId") String orgId);


    /**
     * 更新用户密码
     *
     * @param userAccount 用户帐户
     */
    @Update(value = "UPDATE sys.sys_user SET user_password = #{userPassword} WHERE user_uname = #{userUname}")
    void updateUserPassword(UserAccount userAccount);


    /**
     * 得到合同用户状态
     *
     * @param orgpId orgp id
     * @return {@link Node}
     */
    @Select("SELECT user_status FROM sys.sys_user LEFT JOIN sys.sys_org ON org_id = user_org_id LEFT JOIN sys.sys_orgp ON orgp_org_id = org_id WHERE orgp_id = #{orgpId}")
    Node getContractUserStatus(@Param(value = "orgpId") String orgpId);
}
